﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AccesoDatos;
using AccesoDatos.Manejador;
using System.Runtime.InteropServices;


namespace RMA.Pantallas
{
    public partial class frmReporteGeneral : Form
    {
        [DllImport("user32.dll")]
        static extern bool AnimateWindow(IntPtr hwnd, uint dwTime, uint dwFlags);
        public static DataTable dtResultados;

        public class WinApi
        {
            public static int GWL_EXSTYLE = -20;
            public static int WS_EX_LAYERED = 0x80000;
            public static int LWA_ALPHA = 0x2;
            [DllImport("user32")]
            public static extern int SetLayeredWindowAttributes(IntPtr hWnd, byte crey, byte alpha, int dwFlags);
            [DllImport("user32")]
            public static extern int GetLayeredWindowAttributes(IntPtr hWnd, byte crey, out byte alpha, int dwFlags);
            [DllImport("user32")]
            public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
            [DllImport("user32")]
            public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        }
        void mkwndtrans(IntPtr hWnd, int num)
        {
            int oldWinStyle = WinApi.GetWindowLong(hWnd, WinApi.GWL_EXSTYLE);
            WinApi.SetWindowLong(hWnd, WinApi.GWL_EXSTYLE,
            num < 255 ? oldWinStyle | WinApi.WS_EX_LAYERED :
            oldWinStyle & WinApi.WS_EX_LAYERED);
            WinApi.SetLayeredWindowAttributes(hWnd, 0, (byte)num, WinApi.LWA_ALPHA);
        }
        enum AnimateWindowFlags : uint
        {
            AW_HOR_POSITIVE = 0x00000007,
            AW_HOR_NEGATIVE = 0x00000002,
            AW_VER_POSITIVE = 0x00000004,
            AW_VER_NEGATIVE = 0x00000008,
            AW_CENTER = 0x00000010,
            AW_HIDE = 0x00010000,
            AW_ACTIVATE = 0x00020000,
            AW_SLIDE = 0x00040000,
            AW_BLEND = 0x00080000
        }

        public string strFechaInicio, strFechaFin;
        public frmReporteGeneral()
        {
            InitializeComponent();
        }

        private void btnGenerar_Click(object sender, EventArgs e)
        {
            Manejador_Reportes objRep = new Manejador_Reportes();
            DataTable dtReporte = new DataTable();
            string Asignacion = ""; 
            string Sucursal = "";
            string Proveedor = "";
            string Status = "";
            try
            {
                
                if (chkTodasOrdenes.Checked)
                {
                    strFechaInicio = "19000101";
                    strFechaFin = "19000101";
                }
                else
                {
                    ConvierteFechas();
                }
                if (cbAsignacion.SelectedIndex != -1)
                {
                    Asignacion = cbAsignacion.SelectedValue.ToString();
                }
                if (cbSuc.SelectedIndex != -1)
                {
                    Sucursal = cbSuc.SelectedValue.ToString();
                }
                if (cbStatus.SelectedIndex != -1)
                {
                    Status = cbStatus.SelectedValue.ToString();
                }
                if (cbProveedor.SelectedIndex != -1)
                {
                    Proveedor = cbProveedor.SelectedValue.ToString();
                }
                dtReporte = objRep.RegresaDatosReporteGeneral(strFechaInicio, strFechaFin, Sucursal, int.Parse(Status), Proveedor, txtProducto.Text, txtRma.Text, txtSalida.Text, Asignacion, "0", txtCliente.Text.Trim());
                dtResultados = dtReporte;
                frmVisorReporteGeneral frmV = new frmVisorReporteGeneral(dtReporte, cbStatus.Text);
                AnimateWindow(frmV.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
                frmV.ShowDialog();
            }
            catch (Exception ex)
            { 
                
            }

        }
        public void ConvierteFechas()
        {
            //string FechaI, FechaF;
            string AnioI, MesI, DiaI, AnioF, MesF, DiaF;

            AnioI = dtpFechaI.Value.Year.ToString();
            MesI = dtpFechaI.Value.Month.ToString();
            DiaI = dtpFechaI.Value.Day.ToString();

            AnioF = dtpFechaF.Value.Year.ToString();
            MesF = dtpFechaF.Value.Month.ToString();
            DiaF = dtpFechaF.Value.Day.ToString();

            if (MesI.Length == 1)
            {
                MesI = "0" + MesI;
            }
            if (MesF.Length == 1)
            {
                MesF = "0" + MesF;
            }

            if (DiaI.Length == 1)
            {
                DiaI = "0" + DiaI;
            }
            if (DiaF.Length == 1)
            {
                DiaF = "0" + DiaF;
            }

            strFechaInicio = AnioI + MesI + DiaI;
            strFechaFin = AnioF + MesF + DiaF;

        }
        public void RegresaCatalogos()
        { 
            //DataTables
            DataTable dtEstatus = null;
            DataTable dtSucursales = null;
            DataTable dtProveedor = null;
            DataTable dtAsignaciones = null;

            //Objetos Manejadores
            Manejador_Status objS = new Manejador_Status();
            Manejador_Almacenes objA = new Manejador_Almacenes();
            Manejador_proveedores objP = new Manejador_proveedores();
            Manejador_Status objAsignaciones = new Manejador_Status();


            //Operaciones
            
            dtEstatus = objS.RegresaListadoEstatus();
            cbStatus.DataSource = dtEstatus;
            cbStatus.ValueMember = "estatus_id";
            cbStatus.DisplayMember = "estatus_descripcion";
            

            dtSucursales = objA.RegresaAlmacenes();
            cbSuc.DataSource = dtSucursales;
            cbSuc.DisplayMember = "Nombre";
            cbSuc.ValueMember = "SucId";

            dtProveedor = objP.RegresaProveedores();
            cbProveedor.DataSource = dtProveedor;
            cbProveedor.DisplayMember = "Nombre";
            cbProveedor.ValueMember = "CodPro";


            dtAsignaciones = objAsignaciones.RegresaAsignaciones();
            cbAsignacion.DataSource = dtAsignaciones;
            cbAsignacion.DisplayMember = "Descripcion_asignacion";
            cbAsignacion.ValueMember = "id_Asignacion";
            

        }

        private void frmReporteGeneral_Load(object sender, EventArgs e)
        {
            RegresaCatalogos();
            cbSuc.SelectedIndex = -1;
            cbStatus.SelectedIndex = -1;
            cbProveedor.SelectedIndex = -1;
            cbAsignacion.SelectedIndex = -1;
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void chkTodasOrdenes_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTodasOrdenes.Checked)
            {
                dtpFechaI.Enabled = false;
                dtpFechaF.Enabled = false;
            }
            else
            {
                dtpFechaI.Enabled = true;
                dtpFechaF.Enabled = true;
            }
        }

        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            frmReporteGeneral_Load(sender, e);
            txtProducto.Text = "";
            txtRma.Text = "";
            txtSalida.Text = "";
            txtCliente.Text = "";
            chkTodasOrdenes.Checked = false;
        }
    }
}
